<template>
  <div class="app-container">
    <!-- 顶部查询区域 D:\A_code\a_code\waterui\src\views/dataManage/serviceManage/index.vue  -->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="服务名称：">
        <el-input size="small" v-model="queryForm.name" placeholder="服务名称"></el-input>
      </el-form-item>
      <el-form-item label="发布时间：">
        <el-date-picker size="small" v-model="queryForm.createtime" type="date" placeholder="选择日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button class="query-btn" type="primary" icon="el-icon-search" size="mini"
          @click="handleQuery">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-plus" size="mini" plain v-hasPermi="['system:user:add']"
          @click="handleAdd">新增</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格区域 -->
    <el-table :data="tableData" v-loading="loading" style="width: 100%; margin-top: 20px;">
      <el-table-column prop="index" label="序号">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="服务名称"></el-table-column>
      <el-table-column prop="path" label="接口API"></el-table-column>
      <el-table-column prop="createtime" label="发布时间">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createtime, '{yyyy}-{mm}-{dd} {hh}:{ii}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否启用">
        <template slot-scope="scope">
          {{ scope.row.isenable == 1 ? '启用' : '停用' }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)"
            v-hasPermi="['system:menu:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:menu:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增弹窗/表单区域 -->
    <el-dialog title="新增" :visible.sync="dialogFormVisible">
      <el-form :model="addForm" label-width="150px">
        <el-form-item label="服务名称">
          <el-input v-model="addForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="接口API">
          <el-input v-model="addForm.path" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="数据来源">
          <el-input v-model="addForm.source" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="数据格式">
          <el-input v-model="addForm.datatype" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否启用">
          <el-radio-group v-model="addForm.isenable">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="资源描述">
          <el-input type="textarea" v-model="addForm.apidiscrible"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getServiceList, addServiceItem, editServiceItem, removeServiceItem, getServiceInfo } from '@/api/dataManage/serviceManage';
export default {
  data() {
    return {
      loading: false,
      queryForm: {
        name: '',
        createtime: ''
      },
      tableData: [],
      dialogFormVisible: false,
      addForm: {
        name: '',
        path: '',
        source: '',
        datatype: '',
        isenable: 1,
        apidiscrible: ''
      }
    };
  },
  created() {
    this.query()
  },
  methods: {
    initform() {
      this.addForm = {
        name: '',
        path: '',
        source: '',
        datatype: '',
        isenable: 1,
        apidiscrible: ''
      }
    },
    query() {
      this.getServiceList()
    },
    getServiceList() {
      this.loading = true;
      getServiceList({ ...this.queryForm }).then(res => {
        if (res.code == 200) {
          this.tableData = res.rows
        }
        this.loading = false;
      })
    },
    handleQuery() {
      this.query()
    },
    handleAdd() {
      this.initform()
      this.dialogFormVisible = true;
    },
    handleEdit(row) {
      getServiceInfo(row.id).then(res => {
        if (res.code == 200) {
          // res.data.isenable = res.data.isenable == 1 ? true : false;
          this.addForm = res.data
          this.dialogFormVisible = true;
        }
      })
    },
    handleDelete( row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeServiceItem(row.id).then(res => {
          if (res.code == 200) {
            this.$message.success('删除成功');
            this.query()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })

    },
    handleSubmit() {
      if (this.addForm.id) {
        editServiceItem(this.addForm).then(res => {
          if (res.code == 200) {
            this.$message.success('修改成功');
            this.dialogFormVisible = false;
            this.query()
          }
        })
      } else {
        addServiceItem(this.addForm).then(res => {
          if (res.code == 200) {
            this.$message.success('新增成功');
            this.dialogFormVisible = false;
            this.query()
          }
        })
      }

    }
  }
};
</script>

<style lang="scss" scoped>
.app-container {
  .el-table {
    margin-top: 20px;
  }

  .el-dialog {
    .el-form {
      width: 80%;
      margin: 0 auto;
    }
  }
}
</style>